<?php

class Model_guru extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getAll() {
        $this->db->select('id,nip,password,nama');
        $this->db->from('guru');
        $this->db->order_by('id', 'ASC');
        $query = $this->db->get();

        return $query->result();
    }

    function getGuru($id) {
        $this->db->where('id', $id);
        $query = $this->db->get('guru');

        return $query->row_array();
    }

    function save() {
        $nip = $this->input->post('nip');
        $password = $this->input->post('password');
        $fullname = $this->input->post('nama');
        $data = array(
            'nip' => $nip,
            'password' => md5($password),
            'nama' => $fullname
        );
        
        return $this->db->insert('guru', $data);
    }

    function update() {
        $id = $this->input->post('id');
        $username = $this->input->post('nip');
        $password = $this->input->post('password');
        $fullname = $this->input->post('nama');
        if ($password == '') {
            $data = array(
                'nip' => $username,
                'nama' => $fullname
            );
        } else {
            $data = array(
                'nip' => $username,
                'password' => md5($password),
                'nama' => $fullname
            );
        }

        $this->db->where('id', $id);
        $this->db->update('guru', $data);
    }

}

?>